package com.guchenbo.example.thread;

import com.guchenbo.example.lock.WaitClientCloseServer;

public class JoinDemo {
    public static void main(String[] args) {
        //        demo1();
        demo2();

    }

    /**
     * 线程运行完毕是否自动notify
     */
    private static void demo2() {

        Thread t1 = GcbThreadFactory.create("t1", () -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("t1 done");
        });
        Thread t2 = GcbThreadFactory.create("t2", () -> {
            synchronized (t1) {
                try {
                    System.out.println("t2 wait");
                    t1.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t2 wakeup");
            }
        });
        Thread t3 = GcbThreadFactory.create("t3", () -> {
            synchronized (t1) {
                try {
                    System.out.println("t3 wait");
                    t1.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t3 wakeup");
            }
        });
        t1.start();
        t2.start();
        t3.start();
    }

    private static void demo1() {
        Thread t1 = GcbThreadFactory.create("t1", () -> {
            System.out.println(Thread.currentThread().getName() + " 执行开始 ");
            new WaitClientCloseServer().start(10000);
            System.out.println(Thread.currentThread().getName() + " 执行结束 ");

        });
        Thread t2 = GcbThreadFactory.create("t2", () -> {
            try {
                System.out.println(Thread.currentThread().getName() + " t1.join ");
                t1.join();
                System.out.println(Thread.currentThread().getName() + " 继续执行 ");
                new WaitClientCloseServer().start(10001);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        Thread t3 = GcbThreadFactory.create("t3", () -> {
            try {
                System.out.println(Thread.currentThread().getName() + " t1.join ");
                t1.join();
                System.out.println(Thread.currentThread().getName() + " 继续执行 ");
                new WaitClientCloseServer().start(10002);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        t1.start();
        // t1 join之前需要先启动
        t2.start();
        t3.start();
        synchronized (t1) {
            try {
                System.out.println(Thread.currentThread().getName() + " wait until t1 ");
                t1.wait();
                System.out.println(Thread.currentThread().getName() + " 继续执行");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
